package com.zyb.system.department.service.impl;

import com.zyb.base.service.impl.BaseServiceImpl;
import com.zyb.system.department.mapper.DepartmentMapper;
import com.zyb.system.department.service.IDepartmentService;
import com.zyb.system.department.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        //构建dirPath
        getDirPath(department);
        //更新数据
        departmentMapper.update(department);
    }

    @Transactional
    @Override
    public void remove(Long id) {
        Long count = departmentMapper.loadByParentId(id);
        if(count == 0){
            departmentMapper.delete(id);
           // return;//结束当前方法
        }else {
            throw new RuntimeException("当前部门存在下级部门，不能之间删除");
        }
    }

    @Transactional
    @Override
    public void update(Department department) {
        if(department.getId().equals(department.getParent_id())){
            throw new RuntimeException("不能设置自己的部门为上级部门");
        }
        //构建dirPath
        getDirPath(department);
        departmentMapper.update(department);
    }
    @Override
    public List<Department> getDepartmentTree() {
        //查询全部
        List<Department> departments = departmentMapper.loadAll();

        //获取自己部门
        for (Department department : departments) {
            //判断Parent_id不为空
            if(department.getParent_id() != null){
                //循环父部门
                for (Department parent : departments) {
                    //判断是否是上级部门
                    if(department.getParent_id() == parent.getId()){
                        //获取子部门
                        List<Department> children = parent.getChildren();
                        //添加子部门
                        children.add(department);
                        break;
                    }
                }
            }
        }
        //获取顶级部门
        List<Department> departmentTree = departments.stream()
                .filter(department -> department.getParent_id() == null)//过滤parent_id为空
                .collect(Collectors.toList());//转为list
        return departmentTree;
    }
    //获取dirPath方法
    private void getDirPath(Department department){
        if(department.getParent_id() == null){
            //获取id
            Long id = department.getId();
            //设置dirPath
            department.setDirPath("/"+id);
        }else {
            //获取上级id
            Long parentId = department.getParent_id();
            //通过上级id查询获取到数据
            Department parent = departmentMapper.loadById(parentId);
            //获取dirPath
            String dirPath = parent.getDirPath();
            //拼接真实的dirPath
            department.setDirPath(dirPath+"/"+department.getId());
        }
    }
}
